package com.dhcc.sds.busi.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.config.properties.SysProperties;
import com.dhcc.sds.busi.dao.DrgsAutoMapper;
import com.dhcc.sds.busi.dto.SdsCaseDto;
import com.dhcc.sds.busi.dto.UploadRecordDto;
import com.dhcc.sds.busi.service.IDrgsAutoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.List;
import java.util.Map;

@Service
public class DrgsAutoServiceImpl implements IDrgsAutoService {
    @Autowired
    private SysProperties sysPro;
    @Autowired
    private DrgsAutoMapper drgsAutoMapper;
    @Override
    public List<UploadRecordDto> page(Page<UploadRecordDto> page, Map<String, Object> map) {
        return drgsAutoMapper.caseList(page, map);
    }

    @Override
    public List<UploadRecordDto> caseList(Map<String, Object> map) {
        return drgsAutoMapper.caseList(map);
    }

    @Override
    public String createExcel(Map<String, Object> params) {
        List<UploadRecordDto> excelExport = drgsAutoMapper.caseList(params);
        String filePath = sysPro.getSysHome()+"file_download/sds/uploadreport/";
        File file = new File(filePath);
        if (!file.exists()) file.mkdirs();
        String fileName = "单病种上报病例_" + com.dhcc.core.framework.util.DateUtil.getDays() + ".xlsx";
        File outFile = new File(filePath + fileName);
        try {
            EasyExcel.write(new FileOutputStream(outFile), SdsCaseDto.class).sheet("上报病例列表").doWrite(excelExport);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
//        ExcelExportUtil.exportToFile(filePath + fileName, excelExport);
        return fileName;
    }
}
